//
//  BandingViewController.m
//  workApp
//
//  Created by 林 on 2019/1/11.
//  Copyright © 2019 林. All rights reserved.
#import "BandingViewController.h"
#import "BandingBtn.h"
#import "QRCodeViewController.h"
@interface BandingViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView * tableV;
@property (nonatomic,strong)NSArray * dataArray;
@property (nonatomic,strong)NSMutableArray * familyArray;
@property (nonatomic,strong)UITextField * nameText;
@property (nonatomic,strong)UIView * backView;
@end

@implementation BandingViewController
- (void)getData{
    NSDictionary * dic = @{@"family_id":FamilyID};
    [MDNetDetail GETNet:FamilyMenber_url param:dic block:^(id posts, NSInteger code, NSString *Meg) {
        NSLog(@"%@",posts);
        self.dataArray = posts[@"data"];
        [self.tableV reloadData];
    }] ;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.familyArray = [[NSMutableArray alloc]init];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"绑定机顶盒";
    self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, MDSCREEN_HEIGHT - 100) style:(UITableViewStylePlain)];
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    UIButton * newBoxBtn = [[UIButton alloc]initWithFrame:CGRectMake(70, MDSCREEN_HEIGHT - 80, MDSCREEN_WIDTH - 140, 50)];
    newBoxBtn.backgroundColor = [UIColor colorWithHexStr:@"#6fac45"];
    [newBoxBtn setTitle:@"扫描二维码" forState:(UIControlStateNormal)];
    newBoxBtn.layer.cornerRadius = 25;
    newBoxBtn.layer.masksToBounds = true;
    [newBoxBtn addTarget:self action:@selector(scanClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:newBoxBtn];
    
}
- (void)scanClick
{
    if (self.nameText.text.length <= 0) {
        [MBProgressHUD showInfoMessage:@"请先输入名字"];
        return;
    }
    else if (self.familyArray.count <= 0 )
    {
        [MBProgressHUD showInfoMessage:@"请选择使用该机顶盒的家属"];
        return;
    }
    NSString * str;
    for (int i=0; i<self.familyArray.count; i++) {
        if (i==0) {
        str = [NSString stringWithFormat:@"%@",_familyArray[i]];
        }
        else
        {
        str = [NSString stringWithFormat:@"%@,%@", str,_familyArray[i]];
        }
    }
    
    QRCodeViewController * qrcodeVC = [[QRCodeViewController alloc]init];
    NSString * familyStr;
    for (int i=0 ; i<self.familyArray.count; i++) {
        if (i==0) {
            familyStr = [NSString stringWithFormat:@"%@",self.familyArray[i]];
        }
        else
        {
            familyStr = [NSString stringWithFormat:@"%@,%@",familyStr,self.familyArray[i]];
        }
    }
    qrcodeVC.block = ^(NSString * _Nonnull str) {
        [MBProgressHUD showActivityMessageInView:@""];
        NSDictionary * dic = @{@"box_id":str,@"family_id":FamilyID,@"box_name":self.nameText.text,@"family_member_ids":familyStr};
        [MDNetDetail GETNet:BandingBox_url param:dic block:^(id posts, NSInteger code, NSString *Meg) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showInfoMessage:Meg];
            if (code == 0) {
                [self.navigationController popViewControllerAnimated:true];
            }
        }];
    };
    [self.navigationController pushViewController:qrcodeVC animated:true];
    
    
    
    

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 70;
    }
    else if (indexPath.row == 1)
    {
        return 70;
    }
    else if (indexPath.row == 2)
    {
        return 50;
    }
    else if (indexPath.row == 3)
    {
        return  50+50 * self.dataArray.count/2;
    }
    else
    {
        return 30;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellOne"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellOne"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(30, 30, MDSCREEN_WIDTH-60, 35)];
            label.text = @"1 选择家属< 2 扫描“家庭视频诊所”二维码";
            label.textColor = [UIColor colorWithHexStr:@"#5c5c5c"];
            label.font = [UIFont systemFontOfSize:16];
            label.adjustsFontSizeToFitWidth = true;
            [cell.contentView addSubview:label];
        }
        
        return cell;
    }
    else if (indexPath.row == 1)
    {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellTwo"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellTwo"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
            self.nameText = [[UITextField alloc]initWithFrame:CGRectMake(30, 20, MDSCREEN_WIDTH-60, 45)];
            self.nameText.backgroundColor = [UIColor colorWithHexStr:@"#f3f3f3"];
            self.nameText.layer.borderColor = [UIColor colorWithHexStr:@"#dfdfdf"].CGColor;
            self.nameText.layer.borderWidth = 1;
            self.nameText.textColor = [UIColor colorWithHexStr:@"#5c5c5c"];
            self.nameText.layer.cornerRadius = 5;
            self.nameText.layer.masksToBounds = true;
            self.nameText.font = [UIFont systemFontOfSize:16];
//            nameText.adjustsFontSizeToFitWidth = true;
            self.nameText.textAlignment = NSTextAlignmentCenter;
            self.nameText.placeholder = @"给要绑定的机顶盒取一个名字";
            [cell.contentView addSubview:self.nameText];
        }
        return cell;
    }
    if (indexPath.row == 2) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellThree"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellThree"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(30, 10, MDSCREEN_WIDTH-60, 30)];
            label.text = @"请选择使用该机顶盒的家属";
            label.textColor = [UIColor colorWithHexStr:@"#1b1b1b"];
            label.font = [UIFont systemFontOfSize:16];
            label.adjustsFontSizeToFitWidth = true;
            [cell.contentView addSubview:label];
        }
        return cell;
    }
    if (indexPath.row == 3) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellFour"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellFour"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [self.backView removeFromSuperview];
        self.backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, 50 +50 * self.dataArray.count/2)];
        self.backView.backgroundColor = [UIColor whiteColor];
        [cell.contentView addSubview:self.backView];
        for (int i=0; i<self.dataArray.count; i++) {
            NSDictionary * dic = self.dataArray[i];
            BandingBtn * bandIngBtn = [[BandingBtn alloc]initWithFrame:CGRectMake(40 + (MDSCREEN_WIDTH - 320 + 120)*(i%2), 10 + (50)*(i/2), 120, 45)];
            bandIngBtn.nameLab.text = dic[@"name"];
            bandIngBtn.selected = NO;
            [bandIngBtn addTarget:self action:@selector(chiose:)  forControlEvents:(UIControlEventTouchUpInside)];
            bandIngBtn.tag = 100+i;
            bandIngBtn.backgroundColor = [UIColor colorWithHexStr:@"#f3f3f3"];
            bandIngBtn.nameLab.textColor = [UIColor colorWithHexStr:@"#646464"];
            [self.backView addSubview:bandIngBtn];
        }
        return cell;
    }
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cellFive"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cellfive"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, MDSCREEN_WIDTH-60, 30)];
        label.text = @"提示:使用同一台机顶盒的家属可同时选择后再绑定";
        label.textColor = [UIColor colorWithHexStr:@"#9e9e9e"];
        label.font = [UIFont systemFontOfSize:13];
        label.adjustsFontSizeToFitWidth = true;
        [cell.contentView addSubview:label];
    }

    return cell;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getData];
}
- (void)chiose:(BandingBtn *)btn{
    btn.selected = !btn.selected;
    if (btn.selected == YES) {
        btn.backgroundColor = [UIColor colorWithHexStr:@"#fbe9cc"];
        btn.nameLab.textColor = [UIColor colorWithHexStr:@"#dc7a44"];
        NSDictionary * dic = self.dataArray[btn.tag - 100];
        btn.imageV.image = [UIImage imageNamed:@"gou"];
        [self.familyArray addObject:[NSString stringWithFormat:@"%@",dic[@"member_id"]]];
    }
    else
    {
        btn.backgroundColor = [UIColor colorWithHexStr:@"#f3f3f3"];
        btn.nameLab.textColor = [UIColor colorWithHexStr:@"#646464"];
        btn.imageV.image = [UIImage imageNamed:@""];
        NSDictionary * dic = self.dataArray[btn.tag - 100];
        [self.familyArray removeObject:[NSString stringWithFormat:@"%@",dic[@"member_id"]]];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
